package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.dao.SpuInfoDao;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.SkuItemSaleAttrVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import com.atguigu.gulimall.product.vo.SpuSaveVo;
import com.atguigu.gulimall.product.web.controller.ItemController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.entity.SkuInfoEntity;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;
    
    @Autowired
    SpuInfoDescService spuInfoDescService;
    
    @Autowired
    SpuInfoDao spuInfoDao;
    
    @Autowired
    AttrGroupService attrGroupService;
    
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    
    @Autowired
    ThreadPoolExecutor poolExecutor;
    
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }
    
    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }
    
    /**
     * sku检索
     * key: 
     * catelogId: 225
     * brandId: 9
     * min: 1
     * max: 1
     */
    @Override
    public PageUtils queryPageByCoondition(Map<String, Object> params) {
    
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            lambdaQueryWrapper.and(wrapper -> {
                wrapper.eq(SkuInfoEntity::getSkuId, key).or().likeRight(SkuInfoEntity::getSkuName, key);
            });
        }
    
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            lambdaQueryWrapper.eq(SkuInfoEntity::getCatalogId, catelogId);
        }
    
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            lambdaQueryWrapper.eq(SkuInfoEntity::getBrandId, brandId);
        }
    
        String min = (String) params.get("min");
        if (StringUtils.isNotEmpty(min)) {
            lambdaQueryWrapper.ge(SkuInfoEntity::getPrice, min);
        }
    
        String max = (String) params.get("max");
        if (StringUtils.isNotEmpty(max) && !"0".equalsIgnoreCase(max)) {
            lambdaQueryWrapper.le(SkuInfoEntity::getPrice, max);
        }
    
        IPage<SkuInfoEntity> iPage = this.page(new Query<SkuInfoEntity>().getPage(params), lambdaQueryWrapper);
    
        return new PageUtils(iPage);
    }
    
    @Override
    public List<SkuInfoEntity> getSkuInfoListBySpuId(Long spuId) {
        return this.list(new LambdaQueryWrapper<SkuInfoEntity>().eq(SkuInfoEntity::getSpuId, spuId));
    }
    
    /**
     * 获取sku详情
     */
    @Override
    public SkuItemVo item(Long skuId) {
        try {
            SkuItemVo skuItemVo = new SkuItemVo();
            //1、获取sku基本信息
            CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
                SkuInfoEntity skuInfo = getById(skuId);
                skuItemVo.setSkuInfo(skuInfo);
                return skuInfo;
            }, poolExecutor);
        
            //2、获取sku图片
            CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
                List<SkuImagesEntity> images = skuImagesService.getSkuImagesBySkuId(skuId);
                skuItemVo.setImages(images);
            }, poolExecutor);
        
            //3、获取spu描述信息
            CompletableFuture<Void> spuDescFuture = skuInfoFuture.thenAcceptAsync((skuResult) -> {
                SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(skuResult.getSpuId());
                skuItemVo.setSpuInfoDesc(spuInfoDesc);
            }, poolExecutor);
        
            //4、获取sku销售属性
            CompletableFuture<Void> groupAttrFuture = skuInfoFuture.thenAcceptAsync((skuResult) -> {
                List<SpuItemAttrGroupVo> groupAttrs = attrGroupService.getAttrGroupWithAttrBySpuId(skuResult.getSpuId(), skuResult.getCatalogId());
                skuItemVo.setGroupAttrs(groupAttrs);
            }, poolExecutor);
        
            //5、获取spu规格参数
            CompletableFuture<Void> skuSaleAttrFuture = skuInfoFuture.thenAcceptAsync((skuResult) -> {
                List<SkuItemSaleAttrVo> skuItemSaleAttrVoList = skuSaleAttrValueService.getSaleAttrsBySpuId(skuResult.getSpuId());
                skuItemVo.setSaleAttrs(skuItemSaleAttrVoList);
            }, poolExecutor);
    
    
    
            CompletableFuture.allOf(imageFuture, spuDescFuture, groupAttrFuture, skuSaleAttrFuture).join();
            return skuItemVo;
        } catch (RuntimeException e) {
            e.printStackTrace();
            log.error("product item exception skuId: {}, error: {}", skuId, e.getMessage());
        }
        return new SkuItemVo();
    }
    
    
}